home *** CD-ROM | disk | FTP | other *** search
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- (c) TechInsite Pty. Ltd.
- PO Box 429, Abbotsford, Melbourne. 3067 Australia
- Phone: +61 3 9419 6456
- Fax: +61 3 9419 1682
- Web: www.techinsite.com.au
- EMail: peter_hinrichsen@techinsite.com.au
-
- Created: Jan 2000
-
- Notes: Abstract persistant base classes
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
- unit tiPerObjAbs;
-
- interface
- uses
- tiPtnVisitor
- ;
-
- type
- // The possible states a persistent object can be in:
- TPersistentObjectState = ( posCreate, // The object is new and must be created in the DB
- posRead, // The object has been created, but not filled with data from the DB
- posPK, // The object has been created, but only it's primary key has been read
- posUpdate, // The object has been changed, the DB must be updated
- posDelete, // The object has been deleted, it must be deleted from the DB
- posDeleted, // The object was marked for deletion, and has been deleted in the database
- posClean // The object is 'Clean' no DB update necessary
- ) ;
-
- // Abstract base persistent object
- //----------------------------------------------------------------------------
- TPerObjAbs = class( TVisitedAbs )
- private
- FIntOID : integer ;
- FObjectState : TPersistentObjectState ;
- FIntDispOrder : integer ;
- FOwner: TPerObjAbs;
- function GetDeleted: boolean;
- procedure SetDeleted(const Value: boolean);
- function GetDirty: boolean;
- procedure SetDirty(const Value: boolean);
- protected
- procedure SetOID(const Value: integer); virtual ;
- public
- // Create, and set ObjectState to posClean
- constructor Create ; override ;
- // Create, and assing a new OID
- constructor CreateNew ; virtual ;
- // The object's OID
- property OID : integer read FIntOID write SetOID ;
- // The objects display order, when the object is contained in an ordered list.
- property DispOrder : integer read FIntDispOrder write FIntDispOrder ;
- // The object's state: ie, clean, update, deleted, etc
- property ObjectState : TPersistentObjectState read FObjectState write FObjectState ;
- // An optional back pointer to the owner of the object
- property Owner : TPerObjAbs read FOwner write FOwner ;
- // Has the object been marked as deleted?
- property Deleted : boolean read GetDeleted write SetDeleted ;
- // Is the object dirty? ie its state <> posClean
- property Dirty : boolean read GetDirty write SetDirty ;
- end ;
-
- // A visitor to iterate through an object's owned objects, and mark them for
- // deletion.
- //----------------------------------------------------------------------------
- TVisPerObjDel = class( TVisitorAbs )
- protected
- function AcceptVisitor : boolean ; override ;
- public
- procedure Execute( pVisited : TVisitedAbs ) ; override ;
- end ;
-
- // Iterate over an object's owned objects and determine if any of its owned
- // objects are dirty.
- //----------------------------------------------------------------------------
- TVisPerObjIsDirty = class( TVisitorAbs )
- private
- FbDirty: boolean;
- protected
- function AcceptVisitor : boolean ; override ;
- public
- procedure Execute( pVisited : TVisitedAbs ) ; override ;
- property Dirty : boolean read FbDirty write FbDirty ;
- end ;
-
- implementation
- uses
- dbNextOID
- ,Dialogs // Debug
- ;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TPerObjAbs
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- constructor TPerObjAbs.Create;
- begin
- inherited ;
- ObjectState := posClean ;
- end;
-
- // Create a new instance and get a new OID
- //------------------------------------------------------------------------------
- constructor TPerObjAbs.CreateNew ;
- begin
- Create ;
- OID := gNextOID.NextOID ;
- DispOrder := 0 ;
- ObjectState := posCreate ;
- end;
-
- // Has this object been marked for deletion?
- //------------------------------------------------------------------------------
- function TPerObjAbs.GetDeleted: boolean;
- begin
- result := ( ObjectState = posDelete ) or
- ( ObjectState = posDeleted ) ;
- end;
-
- // Is this object diryt?
- //------------------------------------------------------------------------------
- function TPerObjAbs.GetDirty: boolean;
- var
- lVis : TVisPerObjIsDirty ;
- begin
- lVis := TVisPerObjIsDirty.Create ;
- try
- self.Iterate( lVis ) ;
- result := lVis.Dirty ;
- finally
- lVis.Free ;
- end ;
- end;
-
- // Set all the owned object's state to posDelete
- //------------------------------------------------------------------------------
- procedure TPerObjAbs.SetDeleted(const Value: boolean);
- var
- lVis : TVisPerObjDel ;
- begin
- if Value and not Deleted then begin
- lVis := TVisPerObjDel.Create ;
- try
- self.Iterate( lVis ) ;
- finally
- lVis.Free ;
- end ;
- end ;
- end;
-
- // Set the object's state to posUpdate
- //------------------------------------------------------------------------------
- procedure TPerObjAbs.SetDirty(const Value: boolean);
- begin
- if not ( ObjectState in
- [ posCreate, // The object is new and must be created in the DB
- posUpdate, // The object has been changed, the DB must be updated
- posDelete // The object has been deleted, it must be deleted from the DB
- ]) then
- FObjectState := posUpdate ;
- end;
-
- // Set the OID
- //------------------------------------------------------------------------------
- procedure TPerObjAbs.SetOID(const Value: integer);
- begin
- FIntOID := Value;
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisPerObjDel
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // Only accept the visitor if ObjectState <> posDelete
- function TVisPerObjDel.AcceptVisitor : boolean;
- begin
- result := ( Visited is TPerObjAbs ) and
- ( TPerObjAbs( Visited ).ObjectState <> posDelete ) ;
- end;
-
- // Set ObjectState to posDelete
- //------------------------------------------------------------------------------
- procedure TVisPerObjDel.Execute(pVisited: TVisitedAbs) ;
- begin
- inherited Execute( pVisited ) ;
-
- if not AcceptVisitor then
- exit ; //==>
-
- ( pVisited as TPerObjAbs ).ObjectState := posDelete ;
-
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisPerObjIsDirty
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // Only accept the visitor if the visitor's internal dirty flat is still false
- function TVisPerObjIsDirty.AcceptVisitor: boolean;
- begin
- result := ( Visited is TPerObjAbs ) and
- ( not Dirty ) ;
- end;
-
- // Determine if an object is dirty
- //------------------------------------------------------------------------------
- procedure TVisPerObjIsDirty.Execute(pVisited: TVisitedAbs);
- begin
- inherited Execute( pVisited ) ;
-
- if not AcceptVisitor then
- exit ; //==>
-
- Dirty := TPerObjAbs( pVisited ).ObjectState in
- [ posCreate, // The object is new and must be created in the DB
- posUpdate, // The object has been changed, the DB must be updated
- posDelete // The object has been deleted, it must be deleted from the DB
- ] ;
-
- end;
-
- end.
-